/**
*
* @ author:JiangChencheng
* @ email:744578095@qq.com
* @ data: 2021-11-02 15:51
*/
export default class GunControl extends Laya.Script {

    constructor() {
        super();
        /** @prop {name:sp_ball, tips:"切换小球", type:Node, default:null}*/
        this.sp_ball = null
        /** @prop {name:leftBtn, tips:"选择球的按钮", type:Node, default:null}*/
        this.leftBtn= null
        /** @prop {name:rightBtn, tips:"选择球的按钮", type:Node, default:null}*/
        this.rightBtn = null
        /** @prop {name:ballText, tips:"小球类型的文本", type:Node, default:null}*/
        this.ballText = null
    
        /** @prop {name:ballCountPanel, tips:"特殊小球的数量的文本框", type:Node, default:null}*/
        this.ballCountPanel = null
        /** @prop {name:ballCountText, tips:"特殊小球的数量的文本", type:Node, default:null}*/
        this.ballCountText = null
        /** @prop {name:addBallCountBtn, tips:"特殊小球的添加按钮", type:Node, default:null}*/
        this.addBallCountBtn = null
        /** @prop {name:hintText, tips:"特殊小球效果提示", type:Node, default:null}*/
        this.hintText = null

        // 发射球的类型 0是普通 1强力球 2闪电球
        this.ballType = 0
        this.ballUrlArr = ["UI/ball.png","UI/qlq.png","UI/sdq.png"]
        this.ballTextArr = ["小球","强力球","闪电球"]
        this.hintTextArr = ["","强力球可以造成3点威力","闪电球可造成2点消行效果"]
        this.ballColorArr = ["#a1a1a1","#db54e7","#eadc38"]
        this.ballCountArr = [1000,1000]
    }

    // 减少特殊小球数量
    reduceBallCount(){
        if (this.ballType == 0) return
        this.ballCountArr[this.ballType-1]--
        this.ballCountText.text = this.ballCountArr[this.ballType-1]
        console.log(this.ballCountArr)
    }

    onClose(){
        var data = ""
        for(var i = 0; i < this.ballCountArr.length; i++){
            var element = this.ballCountArr[i]
            if(i != this.ballCountArr.length - 1){
                data+=element+","
            }else {
                data+=element
            }
        }
        Laya.LocalStorage.setItem("BallCount",data)
    }

    onAwake() {


        //监听浏览器关闭的事件码
        Laya.stage.on(Laya.Event.BLUR,this,this.onClose)    

        var data = Laya.LocalStorage.getItem("BallCount")
  
        if(data != null) {
            var dataArr = data.split(",")
            for(var i = 0; i < dataArr.length; i++){
                this.ballCountArr[i] = Number.parseInt(dataArr[i])
            }
        }

        this.ballCountText.text = this.ballCountArr[this.ballType-1]
        this.sp_ball.texture = this.ballUrlArr[this.ballType]
        this.ballText.text = this.ballTextArr[this.ballType]
        this.ballText.color = this.ballColorArr[this.ballType]

        this.rightBtn.on(Laya.Event.CLICK,this,function(){
            this.ballType++
            if(this.ballType > 2) {
                this.ballType = 0
            }
            this.changeBallType()
        })

        this.leftBtn.on(Laya.Event.CLICK,this,function(){
            this.ballType--
            if(this.ballType < 0) {
                this.ballType = 2
            }
            this.changeBallType()
        })

    }

    changeBallType(){
        if(this.ballType > 0) {
            this.ballCountPanel.visible = true
            this.hintText.visible = true
        }else {
            this.ballCountPanel.visible = false
            this.hintText.visible = false
        }
        this.ballCountText.text = this.ballCountArr[this.ballType-1]
        this.sp_ball.texture = this.ballUrlArr[this.ballType]
        this.sp_ball.texture = this.ballUrlArr[this.ballType]
        this.ballText.text = this.ballTextArr[this.ballType]
        this.ballText.color = this.ballColorArr[this.ballType]
        this.hintText.text = this.hintTextArr[this.ballType]
    }
    

    // 隐藏左右切换的按钮
    hideChangeButton(isHidden){
        this.leftBtn.visible = !isHidden
        this.rightBtn.visible = !isHidden
    }
}